package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.app.miniapp.models.WxEvent;
import com.budwk.app.miniapp.models.vo.EventScheduleDetailVO;
import com.budwk.app.miniapp.services.WxeventService;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxEventSchedule;
import com.budwk.app.miniapp.services.WxeventscheduleService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@IocBean
@At("/platform/miniapp/event/schedule")
@ApiDefinition(tag = "微信小程序-事件提醒计划")
@Slf4j
public class WxeventscheduleController {
    @Inject
    private WxeventscheduleService wxEventScheduleService;
    @Inject
    private WxeventService wxeventService;


    @At("/list")
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询事件提醒计划")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxEventSchedule.class
    )
    @ApiResponses(
            implementation = WxEventSchedule.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxEventSchedule bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        WxEvent wxEvent= wxeventService.fetch(bean.getEventId());
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getEventId() != null && Strings.isNotBlank(bean.getEventId().toString())) {
            cnd.and("event_id", "=", bean.getEventId());
        }

        if (bean.getEventType() != null && Strings.isNotBlank(bean.getEventType().toString())) {
            cnd.and("event_type", "=", bean.getEventType());
        }

        if (bean.getEventTime() != null && Strings.isNotBlank(bean.getEventTime().toString())) {
            cnd.and("event_time", "=", bean.getEventTime());
        }

        if (bean.getEventContent() != null && Strings.isNotBlank(bean.getEventContent().toString())) {
            cnd.and("event_content", "=", bean.getEventContent());
        }

        if (bean.getEventContentJson() != null && Strings.isNotBlank(bean.getEventContentJson().toString())) {
            cnd.and("event_content_json", "=", bean.getEventContentJson());
        }

        if (bean.getEventRemark() != null && Strings.isNotBlank(bean.getEventRemark().toString())) {
            cnd.and("event_remark", "=", bean.getEventRemark());
        }

        if (bean.getEventStatus() != null && Strings.isNotBlank(bean.getEventStatus().toString())) {
            cnd.and("event_status", "=", bean.getEventStatus());
        }
        cnd.desc(WxEventSchedule::getCreatedAt);
        Pagination pagination= wxEventScheduleService.listPage(pageNo, pageSize, cnd);
        ((List<WxEventSchedule>) pagination.getList()).forEach(s->{
              s.setVolunteerWhetherHelp(wxEvent.getVolunteerWhetherHelp());
          });
        return Result.data(pagination);
    }

    /**
     * 导出事件提醒列表
     */
 /*   @SaCheckPermission("weixin:eventSchedule:export")
    @Log(title = "事件提醒", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxEventScheduleBo bo, HttpServletResponse response) {
        List<WxEventScheduleVo> list = iWxEventScheduleService.queryList(bo);
        ExcelUtil.exportExcel(list, "事件提醒", WxEventScheduleVo.class, response);
    }*/

    @At("/add")
    @Ok("json")
    @POST
    @ApiOperation(name = "新增事件提醒计划")
    @ApiFormParams(
            implementation = WxEventSchedule.class
    )
    @ApiResponses
    @SLog(tag = "新增事件提醒计划", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxEventSchedule wxEventSchedule, HttpServletRequest req) {
        wxEventSchedule.setCreatedBy(SecurityUtil.getUserId());
        wxEventScheduleService.insert(wxEventSchedule);
        return Result.success();
    }

    @At("/upadte")
    @Ok("json")
    @POST
    @ApiOperation(name = "修改事件提醒计划")
    @ApiFormParams(
            implementation = WxEventSchedule.class
    )
    @ApiResponses
    @SLog(tag = "修改事件提醒计划", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxEventSchedule wxEventSchedule, HttpServletRequest req) {
        wxEventSchedule.setUpdatedBy(SecurityUtil.getUserId());
        wxEventScheduleService.updateIgnoreNull(wxEventSchedule);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取事件提醒计划")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxEventSchedule wxEventSchedule = wxEventScheduleService.fetch(id);
        if (wxEventSchedule == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxEventSchedule);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除事件提醒计划")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除事件提醒计划", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxEventSchedule wxEventSchedule = wxEventScheduleService.fetch(id);
        if (wxEventSchedule == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxEventScheduleService.delete(id);
        req.setAttribute("_slog_msg", wxEventSchedule.getId());
        return Result.success();
    }

    /**
     * 根据事件id查询事件调度情况
     * 供小程序使用
     *
     * @param eventId 事件id
     * @return 结果
     */
    @At("/listEventScheduleDetail")
    @Ok("json")
    @POST
    @ApiOperation(name = "根据事件id查询事件调度情况")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "eventId", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "根据事件id查询事件调度情况", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> listEventScheduleDetail(@Param("eventId") String eventId) {
        List<EventScheduleDetailVO> resultList = wxEventScheduleService.listEventScheduleDetail(eventId);
        return Result.success(resultList);
    }
}
